package com.lwz.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lwz.order.entity.Order;
import com.lwz.order.service.OrderService;
import com.lwz.util.JwtInfo;
import com.lwz.util.JwtUtil;
import com.lwz.util.R;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author Lw中
 * @since 2020-11-20
 */
@RestController
@RequestMapping("/order/order")
@CrossOrigin
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
     * 通过课程id和用户id生成订单接口，返回订单号
     * @param courseId
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/addOrder/{courseId}")
    public R addOrder(@PathVariable String courseId, HttpServletRequest httpServletRequest) {
        JwtInfo jwtInfo = JwtUtil.getMemberIdByJwtToken(httpServletRequest);
        String userId = jwtInfo.getId();
        String orderId =  orderService.addCourseOrder(courseId, userId);
        return R.ok().data("orderId", orderId);
    }

    /**
     * 通过订单号查询订单接口
     * @param orderId
     * @return
     */
    @GetMapping("/getOrder/{orderId}")
    public R getOrder(@PathVariable String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("order",order);
    }

    /**
     * 根据课程id和用户id查询订单表状态
     * @param courseId
     * @param memberId
     * @return
     */
    @GetMapping("/isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId, @PathVariable String memberId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        //支付状态 1代表已经支付
        wrapper.eq("status",1);
        int count = orderService.count(wrapper);
        //已经支付
        if(count>0) {
            return true;
        } else {
            return false;
        }
    }

}

